
// 读取静态的文件数据
export const read_avatar = async (file_path) =>{

  const response = await fetch(file_path);
  const buffer = await response.arrayBuffer();
  return new Uint8Array(buffer);
};



export const tobase64String = (Uint8Data) => {
    const base64String = btoa(
        Uint8Data.reduce((data, byte) => data + String.fromCharCode(byte), '')
    );
    const avatar = `data:image/jpeg;base64,${base64String}`;
    return avatar;
}

export const toArrayBuffer = (base64) => {
    // 移除base64字符串中的data:image/jpeg;base64,前缀
    const base64Data = base64.split(',')[1];
    // 将base64转换为二进制字符串
    const binaryString = atob(base64Data);
    // 创建Uint8Array来存储二进制数据
    const bytes = new Uint8Array(binaryString.length);
    // 将每个字符转换为对应的字节值
    for (let i = 0; i < binaryString.length; i++) {
        bytes[i] = binaryString.charCodeAt(i);
    }
    return bytes;
}
